package com.yyw.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyw.bean.Notice;
import com.yyw.bean.User;
import com.yyw.dao.NoticeDao;
import com.yyw.util.MyContext;
import com.yyw.util.Result;
import com.yyw.util.ResultFactory;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "公告API")
@RestController
@RequestMapping("/user")
@Slf4j
public class NoticeController {

    @Autowired
    private NoticeDao noticeDao;

    // 教师发布公告
    @CrossOrigin
    @ApiOperation(value = "教师发布公告", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "classid", value = "当班级id", required = true),
            @ApiImplicitParam(name = "title", value = "标题", required = true),
            @ApiImplicitParam(name = "content", value = "内容", required = true) })
    @PostMapping("/saveNotice")
    public Result saveNotice(@RequestParam("classid") Long classid, @RequestParam("title") String title,
            @RequestParam("content") String content) {
        if (classid == null || StringUtils.isEmpty(title) || StringUtils.isEmpty(content)) {
            return ResultFactory.notice("数据不合法");
        }
        Notice notice = new Notice();
        notice.setClassid(classid);
        notice.setTitle(title);
        notice.setContent(content);
        int i = noticeDao.saveNotice(notice);
        if (i > 0) {
            return ResultFactory.success("发布成功");
        } else {
            return ResultFactory.failure("发布失败");
        }
    }

    // 教师删除公告
    @CrossOrigin
    @ApiOperation(value = "教师删除公告", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "公告id", required = true) })
    @PostMapping("/deleteNotice")
    public Result deleteNotice(@RequestParam("id") Long id) {
        if (id == null) {
            return ResultFactory.notice("数据不合法");
        }
        int i = noticeDao.deleteNotice(id);
        if (i > 0) {
            return ResultFactory.success("删除成功");
        } else {
            return ResultFactory.failure("删除失败");
        }
    }

    // 教师修改公告
    @CrossOrigin
    @ApiOperation(value = "教师修改公告", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "公告id", required = true),
            @ApiImplicitParam(name = "title", value = "标题", required = true),
            @ApiImplicitParam(name = "content", value = "内容", required = true) })
    @PostMapping("/updateNotice")
    public Result updateNotice(@RequestParam("id") Long id, @RequestParam("title") String title,
            @RequestParam("content") String content) {
        if (id == null || StringUtils.isEmpty(title) || StringUtils.isEmpty(content)) {
            return ResultFactory.notice("数据不合法");
        }
        Notice notice = new Notice();
        notice.setId(id);
        notice.setTitle(title);
        notice.setContent(content);
        int i = noticeDao.updateNotice(notice);
        if (i > 0) {
            return ResultFactory.success("修改成功");
        } else {
            return ResultFactory.failure("修改失败");
        }
    }

    // 教师获取指定班级的公告，分页
    @CrossOrigin
    @ApiOperation(value = "分页获取指定班级公告", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNo", value = "当前页数", required = false),
            @ApiImplicitParam(name = "pageNum", value = "每页数量", required = false) })
    @GetMapping("/getClassNoticesByClassId")
    public Result getClassNoticesByClassId(
            @RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(value = "pageNum", defaultValue = "10", required = false) Integer pageNum,
            @RequestParam(value = "classid") Long classid) {
        if (pageNo < 0 || pageNum < 0) {
            return ResultFactory.notice("数据不合法");
        }

        if (classid == null) {
            return ResultFactory.notice("数据不合法");
        }

        PageHelper.startPage(pageNo, pageNum);
        List<Notice> notices = noticeDao.getNotices(classid);
        PageInfo<Notice> pageInfo = new PageInfo<>(notices);

        return ResultFactory.success(pageInfo);
    }

    @CrossOrigin
    @ApiOperation(value = "获取指定公告的详细信息", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "noticeid", value = "公告id", required = true) })
    @GetMapping("/getNoticById")
    public Result getNoticById(@RequestParam("noticeid") Long noticeid) {
        if (noticeid == null) {
            return ResultFactory.notice("数据不合法");
        }

        Notice notice = noticeDao.getNoticeById(noticeid);
        return ResultFactory.success(notice);
    }

    @CrossOrigin
    @ApiOperation(value = "分页获取当前班级公告", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNo", value = "当前页数", required = false),
            @ApiImplicitParam(name = "pageNum", value = "每页数量", required = false) })
    @GetMapping("/getClassNotices")
    public Result getClassNotices(@RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(value = "pageNum", defaultValue = "10", required = false) Integer pageNum) {
        if (pageNo < 0 || pageNum < 0) {
            return ResultFactory.notice("数据不合法");
        }

        User user = MyContext.getSessionUser();
        if (user.getClassid() == null) {
            return ResultFactory.notice("未加入班级");
        }

        PageHelper.startPage(pageNo, pageNum);
        List<Notice> notices = noticeDao.getNotices(user.getClassid());
        PageInfo<Notice> pageInfo = new PageInfo<>(notices);

        return ResultFactory.success(pageInfo);
    }
}
